package com.dianzi.certification.controller;

import com.dianzi.certification.entity.CertAttachment;
import com.dianzi.certification.service.CertAttachmentService;
import com.dianzi.certification.utils.JwtUtils;
import com.dianzi.certification.utils.TimeUtils;
import com.dianzi.certification.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @author copa
 * @createDate 2022-03-31 16:12
 * @function
 */
@RestController
@Api(tags = {"文件处理"})
public class CertAttachmentController {

    @Resource
    private CertAttachmentService certAttachmentService;

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @PostMapping("file/upload")
    @ApiOperation(value = "文件上传（返回文件id）")
    public R<Integer> upload(MultipartFile multiFile, HttpSession session) {
        return certAttachmentService.upload(multiFile, session);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @GetMapping("file/info/{attachmentId}")
    @ApiOperation(value = "查看文件信息")
    public R<CertAttachment> fileInfo(@ApiParam("认证阶段：1-认证申请 2-专业自评 3-进校考察 5-整改提高") @RequestParam Integer certStage,
                                      @ApiParam("文件id") @PathVariable("attachmentId") Integer attachmentId,
                                      HttpServletRequest request) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return certAttachmentService.getFileInfo(certStage, attachmentId, userId);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_PRINCIPAL')")
    @GetMapping("file/download/{attachmentId}")
    @ApiOperation(value = "文件下载")
    public ResponseEntity<byte[]> fileDownload(@ApiParam("认证阶段：1-认证申请 2-专业自评 3-进校考察 5-整改提高") @RequestParam Integer certStage,
                                               @ApiParam("文件id") @PathVariable("attachmentId") Integer attachmentId,
                                               HttpServletRequest request,
                                               HttpSession session) {
        Integer userId = JwtUtils.getUserId(request.getHeader("Authorization"));
        return certAttachmentService.fileDownload(certStage, attachmentId, userId, session);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_EXPERT')")
    @GetMapping("admin/file/download/{attachmentId}")
    @ApiOperation(value = "文件下载(认证专家)")
    public ResponseEntity<byte[]> fileDownloadAdmin(@ApiParam("文件id") @PathVariable("attachmentId") Integer attachmentId,
                                               HttpSession session) {
        return certAttachmentService.fileDownloadAdmin(attachmentId, session);
    }

}
